package com.marketingexpert.controller.publicmgnt.tx;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.service.snsmgnt.AccessToken;
import com.marketingexpert.service.snsmgnt.RequestToken;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.util.WebUtils;

public class TencentAuthCallbackController implements Controller {

	private SnsServiceFactory snsServiceFactory;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		SnsService snsService = snsServiceFactory
				.getService(SnsServiceFactory.TECENT);

		RequestToken reqToken = (RequestToken) request.getSession()
				.getAttribute("resToken");

		if (reqToken != null) {
			String verifier = request.getParameter("oauth_verifier");

			if (verifier != null) {
				AccessToken accessToken = snsService.gettAccessToken(reqToken,
						verifier);
				if (accessToken != null) {
					request.getSession().setAttribute("txAccessToken",
							accessToken);
					request.getSession().setAttribute("weiboUserId",
							accessToken.getUserId());
					response
							.sendRedirect(WebUtils.getBasepath(request)
									+ "/unsec/t/search.do?pCat=0&qCat=00&page=1&catCur=sAll&cur=li_all");
					return null;
				}
			}
		}
		response.sendRedirect(WebUtils.getBasepath(request)
						+ "/unsec/t/search.do?pCat=0&qCat=00&page=1&catCur=sAll&cur=li_all");
		return null;
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}

}
